package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.SearchSyslogResponse;
import com.lczc.soa.domain.Syslog;
import com.lczc.soa.domain.SyslogExample;
import com.lczc.soa.mapper.SyslogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SyslogServiceImpl implements SyslogService {

    @Autowired
    private SyslogMapper syslogMapper;

    @Override
    public int saveSyslog(Syslog syslog) {
        if(syslog.getId()!=null){
            return syslogMapper.updateByPrimaryKeySelective(syslog);

        }else {
            return syslogMapper.insert(syslog);
        }
    }

    @Override
    public SearchSyslogResponse findAllSyslog(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Syslog> syslogs = syslogMapper.selectByExample(new SyslogExample());
        System.out.println("本次查询日志列表 ："+syslogs);
        return new SearchSyslogResponse(((Page)syslogs).getTotal(),syslogs);
    }

    public Syslog findSyslogByPrimaryKey(String id) {
        return syslogMapper.selectByPrimaryKey(id);
    }

    public int deleteSyslog(String id) {
        return syslogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SearchSyslogResponse findSyslogLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Syslog> Syslogs = syslogMapper.selectByLikeName(keywords);
        return new SearchSyslogResponse(((Page)Syslogs).getTotal(),Syslogs);
    }

    @Override
    public List<Syslog> listSyslog() {
        return syslogMapper.selectAll();
    }

    @Override
    public Syslog findSyslogByName(String userName) {
        return syslogMapper.selectByName(userName);
    }
}
